package com.np.permission.demo1.utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;

public class PermissionX {

    // 是否全部授权
    private static boolean isAllGranted(Activity activity, String[] permissions) {

        // 这个判断是因为6.0之前的权限不需要用户同意
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Integer checkResult;
            for (String permission : permissions) {
                checkResult = ActivityCompat.checkSelfPermission(activity, permission);
                if (checkResult != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }

        return true;
    }

    // 申请权限（注意：”禁止后不再提示“的权限，不会再弹出要用户授权的提示）
    private static void requestPermission(Activity activity, String[] permissions, int requestCode) {
        ActivityCompat.requestPermissions(activity, permissions, requestCode);
    }

    // 检查权限
    public static void checkPermission(Activity activity, String[] permissions, int requestCode) {
        if (!isAllGranted(activity, permissions)) {
            requestPermission(activity, permissions, requestCode);
        }
    }
}
